package com.zr.d_c.serviceimpl;

import java.util.List;

import com.zr.d_c.dao.FuncDao;
import com.zr.d_c.dao.RoleDao;
import com.zr.d_c.daoimpl.FuncDaoImpl;
import com.zr.d_c.daoimpl.RoleDaoImpl;
import com.zr.d_c.model.Func;
import com.zr.d_c.service.FuncService;

import net.sf.json.JSONArray;

public class FuncServiceImpl implements FuncService {

	FuncDao funcdao = new FuncDaoImpl();
	RoleDao roledao = new RoleDaoImpl();
	public JSONArray getCurrentUserFuncsByPartentId(int parentid, int rid) {
		// TODO Auto-generated method stub
		String ids = roledao.selectRoleByRid(rid).getFuncs();
		JSONArray userFuncs = funcdao.selectFuncByParentIdAndFids(parentid, ids);
		for (int i = 0; i < userFuncs.size(); i++) {
			String state = userFuncs.getJSONObject(i).getString("state");
			if("open".equals(state)){
				continue;
			}else{
				int currentId = userFuncs.getJSONObject(i).getInt("id");
				JSONArray children = this.getCurrentUserFuncsByPartentId(currentId, rid);
				userFuncs.getJSONObject(i).put("children", children);
			}
		}
		
		return userFuncs;
	}
	public JSONArray getAllFuncsByPartentId(int parentid, int rid) {
		// TODO Auto-generated method stub
		String ids = roledao.selectRoleByRid(rid).getFuncs();
		JSONArray userFuncs = funcdao.selectAllFuncByParentId(parentid, ids);
		for (int i = 0; i < userFuncs.size(); i++) {
			String state = userFuncs.getJSONObject(i).getString("state");
			if("open".equals(state)){
				continue;
			}else{
				int currentId = userFuncs.getJSONObject(i).getInt("id");
				JSONArray children = this.getAllFuncsByPartentId(currentId, rid);
				userFuncs.getJSONObject(i).put("children", children);
			}
		}
		
		return userFuncs;
	}
	public JSONArray getAllFuncsByPartentId(int parentid) {
		// TODO Auto-generated method stub
		JSONArray userFuncs = funcdao.selectAllFuncByParentId(parentid);
		for (int i = 0; i < userFuncs.size(); i++) {
			String state = userFuncs.getJSONObject(i).getString("state");
			if("open".equals(state)){
				continue;
			}else{
				int currentId = userFuncs.getJSONObject(i).getInt("id");
				JSONArray children = this.getAllFuncsByPartentId(currentId);
				userFuncs.getJSONObject(i).put("children", children);
			}
		}
		
		return userFuncs;
	}
	public Func getFuncByFname(String fname) {
		// TODO Auto-generated method stub
		Func f = new Func();
		f = funcdao.selectFuncByFname(fname);
		if(f.getFname() != null){
			return f;
		}
		return null;
	}
	public int deleteFuncByFname(String fname) {
		// TODO Auto-generated method stub
		return funcdao.deleteFuncByFname(fname);
	}
	public int addFunc(Func f) {
		// TODO Auto-generated method stub
		return funcdao.insertFunc(f);
	}
	public List<Func> getAllFunc() {
		// TODO Auto-generated method stub
		return funcdao.selectAllFunc();
	}
	@Override
	public List<Func> getFirstFunc() {
		// TODO Auto-generated method stub
		return funcdao.selectFirstFunc();
	}
	@Override
	public List<Func> getNotFirstFunc() {
		// TODO Auto-generated method stub
		List<Func> f1 = funcdao.selectAllFunc();
		List<Func> f2 = funcdao.selectFirstFunc();
		f1.removeAll(f2);
		return f1;
	}

}
